const {
    model
} = require('mongoose');
const {
    getOrders,
    deleteOrders,
    modifyOrders,
    getOrdersByState,
    getOrdersByTenantsName,
    getOrdersById,
    getOrdersByParking,
    addEvaluate,
    getHistoryOrders
} = require('../dao/ordersDao');

module.exports.getOrders = async function (params) {
    let msg;
    const {
        searchBy,
        searchValue
    } = params;
    if (searchBy == 'state') { //分类查找订单
        msg = await getOrdersByState(params);
        if (searchValue == 0) { //查看全部订单
            msg = await getOrders(params);
        }
    } else if (searchBy == 'tenantsName') { //按顾客姓名查找
        msg = await getOrdersByTenantsName(params);
    } else if (searchBy == '_id') { //按订单编号查找
        msg = await getOrdersById(params);
    } else if (searchBy == 'parkingsAddress') { //按车位地址查找
        msg = await getOrdersByParking(params);
    } else {
        //无搜索条件，全部订单
        msg = await getOrders(params);
    }
    //console.log('获取订单', msg);
    return msg;
}

module.exports.deleteOrders = async function (data) {
    const msg = await deleteOrders(data);

    if (msg.ok) {
        return ({
            status: 1,
            msg,
            message: '订单删除成功'
        });
    } else {
        return ({
            status: 0,
            message: '订单删除失败'
        });
    }
}
module.exports.modifyOrders = async function (data) {
    const msg = await modifyOrders(data);
    if (msg.ok) {
        return ({
            status: 1,
            msg,
            message: '修改成功'
        });
    } else {
        return ({
            status: 0,
            message: '修改失败'
        })
    }
}

//获取历史订单   廖冬  getHistoryOrders
module.exports.getHistoryOrders = async function (val) {
    console.log(2222222222222222222, val);
    const msg = await getHistoryOrders(val);
    if (msg.data.length > 0) {
        return {
            status: 1,
            message: "获取历史订单成功",
            msg
        }
    } else {
        return {
            status: 0,
            message: "获取历史订单失败",
            msg
        }
    }
}

//新增评价内容    廖冬addEvaluate

module.exports.addEvaluate = async function (data) {
    const msg = await addEvaluate(data);
    if (msg.ok) {
        return ({
            status: 1,
            msg,
            message: '新增评价成功'
        });
    } else {
        return ({
            status: 0,
            message: '新增评价失败'
        })
    }
}